<?php
namespace helper;
use \helper\Arr;
use \xg\facade\Cookie;
use \xg\facade\Session as XgSession;

class Session {
    protected static $session = null;
	public static function prefix($prefix) {
		XgSession::prefix($prefix);
		if(substr($prefix,-1)!=='_'){
			$prefix.='_';
		}
		Cookie::prefix($prefix);
	}
	public static function get($name,$default=null) {
		return XgSession::get($name,$default);
	}
	public static function remove($name) {
		XgSession::remove($name);
		Cookie::remove($name);
	}
	public static function set($name,$data) {
		return XgSession::set($name,$data);
	}
	protected static function cookieLogin() {
		if(!self::get('user_data')) {
			$data=Cookie::get('user_data');
			if($data) {
                self::set('user_data',$data);
			}
		}
	}
	public static function userid() {
		$user=self::login();
		if($user){
            return $user['userid'];
        }
		return 0;
	}
	public static function username() {
		$user=self::login();
		if($user){
            return $user['username'];
        }
		return '';
	}
	public static function group() {
		$user=self::login();
		if($user){
            return $user['group'];
        }
		return '';
	}
	public static function login() {
		self::cookieLogin();
		return self::get('user_data');
	}
	public static function logout() {
		self::remove('user_data');
	}
	public static function setLogin($userid,$username='',$group=1,$remember=true) {
		$data=array('userid'=>$userid,'username'=>$username,'group'=>$group);
		self::set('user_data',$data);
        if($remember) {
            unset($data['sign']);
            $data['sign']=Arr::sign($data);
            Cookie::set('user_data',$data,['expire'=>365*24*60*60]);
        }
	}
}	
?>